// pages/index/search/search.js
const tools = require('../../../utils/tool')
Page({

  data: {
    curTab: 0,
    isShow: true,
    searchVal: '', //输入框内容
    historyList: [], //搜索历史数据
    hotList: ['减脂故事', '减脂故事', '减脂故事', '减脂故事'], //搜索热门
    searchResult: [], //搜索
    userList: [], //搜索用户结果
    noResult: false
  },
  // 全部和用户切换
  bindSelect(e) {
    let curTab = e.currentTarget.dataset.index
    this.setData({
      curTab: curTab,
      searchResult:[]
    })
    if (curTab == 0) {
      this.SearchResult(2)
    } else {
      this.SearchResult(1)
    }

  },
  // 输入搜索框内容
  bindSearch(e) {
    this.setData({
      searchVal: e.detail.value
    })
    if (e.detail.value == '') {
      this.setData({
        isShow: true
      })
    }
  },
  // 清除搜索框
  bindClearSearch() {
    this.setData({
      searchVal: '',
      isShow: true
    })
  },
  //  清空搜索历史
  bindClear() {
    this.setData({
      historyList: []
    })
    wx.clearStorageSync("history")
  },
  // 搜索热门词
  bindHotItem(e) {
    let item = e.currentTarget.dataset.item
    this.setData({
      searchVal: item,
      searchResult:[]
    })
    if (this.data.curTab == 0) {
      this.SearchResult(2)
    } else {
      this.SearchResult(1)
    }
  },    
  onLoad() {
    this.gethotsearch()
  },
  onShow() {
    if (wx.getStorageSync('history') != '') {
      this.setData({
        historyList: wx.getStorageSync('history')
      })
    }
  },
  // 获取搜索结果
  bindSearchResult() {
    this.setData({
      searchResult:[]
    })
    var historyList = this.data.historyList
    historyList.unshift(this.data.searchVal)
    if (historyList.length >= 6) {
      historyList.pop()
    }
    wx.setStorageSync('history', historyList)
    this.SearchResult(2)
    if (this.data.curTab == 0) {
      this.SearchResult(2)
    } else {
      this.SearchResult(1)
    }
  },
  SearchResult(type) {
    console.log(this.data.searchVal)
    if (this.data.searchVal == '') {
      tools.showToast('请输入搜索关键字', 'none')
      return
    }
    this.setData({
      isShow: false
    })
    tools.showLoading(type)
    let hot = tools.request('/api/applets/user/search', 'get', {
      is_user: type,
      content: this.data.searchVal
    }, wx.getStorageSync('token'))
    hot.then(res => {
        console.log(res)
        wx.hideLoading()
        if (res.data.status) {
          if (res.data.list.data.length == 0) {
            this.setData({
              noResult: true
            })
          } else {
            this.setData({
              searchResult: res.data.list.data,
              noResult: false
            })
          }
        } else {
          tools.showToast(res.data.error_msg, 'error')
        }
      })
      .catch(err => {
        tools.showToast(err.errMsg, 'error')
      })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  // 获取热门搜索
  gethotsearch() {
    tools.showLoading()
    let hotsearch = tools.request('/api/applets/search/hot', 'get', {}, wx.getStorageSync('token'))
    hotsearch.then(res => {
      wx.hideLoading()
      if (res.data.status) {
        this.setData({
          hotList: res.data.list
        })
      }
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },
  onShareAppMessage: function () {

  }
})